package com.jvtd.demo.dropDownMenu;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jvtd.bean.DropDownMenuData;
import com.jvtd.bean.ProvincesBean;
import com.jvtd.demo.R;
import com.jvtd.utils.ProvincesUtils;
import com.jvtd.widget.dialog.JvtdAlertDialog;
import com.jvtd.widget.dropDownMenu.JvtdDropDownMenu;
import com.jvtd.widget.toast.JvtdToast;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DropDownMenuActivity extends AppCompatActivity {

    private int leftPos = 0;
    private int rightPos = 0;
    private List<DropDownMenuBean> mOneBeans = new ArrayList<>();
    private List<List<DropDownMenuBean>> mTwoBeans = new ArrayList<>();

    private int pIndex = 0;
    private List<String> mStrings = new ArrayList<>();

    @SuppressLint("DefaultLocale")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drop_down_menu);
        TextView ad1Btn = findViewById(R.id.btn_1);
        TextView ad2Btn = findViewById(R.id.btn_2);

        ad2Btn.setOnClickListener(view -> showTwo(ad2Btn));
        ad1Btn.setOnClickListener(view -> showOne(ad1Btn));

        Random random = new Random();
        for (int i=0;i<random.nextInt(100);i++){
            mStrings.add(String.format("测试数据%d", i));
        }

        ProvincesUtils.getInstance(this).getProvincesData(new ProvincesUtils.OnObtainListener() {
            @Override
            public void onObtainSuccess(List<ProvincesBean> provincesBeans, List<List<ProvincesBean>> cityBeans, List<List<List<ProvincesBean>>> areaBeans) {
                int index = 0;
                for (ProvincesBean provincesBean : provincesBeans) {
                    mOneBeans.add(new DropDownMenuBean(provincesBean));
                    List<DropDownMenuBean> menuBeans = new ArrayList<>();
                    for (ProvincesBean bean : cityBeans.get(index)) {
                        menuBeans.add(new DropDownMenuBean(bean));
                    }
                    mTwoBeans.add(menuBeans);
                    index++;
                }
            }

            @Override
            public void onObtainFailed() {

            }
        });
    }

    private void showTwo(TextView view) {
        new JvtdDropDownMenu.Builder<DropDownMenuBean>().setContext(this)
                .setHeightPer(0.7f)
                .setRightPer(0.7f)
                .setLeftBgColor(R.color.color_999)
                .setRightBgColor(R.color.color_fff)
                .setLineColor(R.color.color_333)
                .setLeftTextBuilder(new JvtdDropDownMenu
                        .TextBuilder()
                        .setNormalTextColor(R.color.color_333)
                        .setSelectTextColor(R.color.color_fff)
                .setNormalBgColor(R.color.color_fff)
                .setSelectBgColor(R.color.color_333)
                .setHeight(56))
                .setRightTextBuilder(new JvtdDropDownMenu.TextBuilder()
                .setNormalTextColor(R.color.color_999)
                .setSelectTextColor(R.color.color_333)
                .setTextLeft(false))
                .setTopView(view)
                .setType(JvtdDropDownMenu.TWO)
                .setOnTwoListener(new JvtdDropDownMenu.OnTwoListener<DropDownMenuBean>() {
                    @Override
                    public void onLeftItemClick(int pos) {
                        leftPos = pos;
                        rightPos = 0;
                    }

                    @Override
                    public void onRightItemClick(int lpos, int rpos) {
                         leftPos = lpos;
                         rightPos = rpos;
                         view.setText(mTwoBeans.get(leftPos).get(rightPos).getText());
                    }

                    @Override
                    public List<DropDownMenuBean> getLeftData() {
                        return mOneBeans;
                    }

                    @Override
                    public List<DropDownMenuBean> getRightData(int leftPos) {
                        return mTwoBeans.get(leftPos);
                    }

                    @Override
                    public int getCurrentLeftSelect() {
                        return leftPos;
                    }

                    @Override
                    public int getCurrentRightSelect() {
                        return rightPos;
                    }
                }).build().show();
    }

    private void showOne(TextView view) {
        new JvtdDropDownMenu.Builder<String>().setContext(this)
                .setHeightPer(0.4f)
                .setRightBgColor(R.color.color_fff)
                .setRightTextBuilder(new JvtdDropDownMenu.TextBuilder().setTextLeft(false))
                .setTopView(view)
                .setType(JvtdDropDownMenu.ONE)
                .setOnOneListener(new JvtdDropDownMenu.OnOneListener<String>() {
                    @Override
                    public void onItemClick(int pos) {
                        pIndex = pos;
                        view.setText(mStrings.get(pIndex));
                    }

                    @Override
                    public List<String> getData() {
                        return mStrings;
                    }

                    @Override
                    public int getCurrentSelect() {
                        return pIndex;
                    }
                }).build().show();
    }
}

